Dropout

在训练期间对输入张量应用 Dropout 操作。它根据给定的 mask 将输入张量中的部分元素置零,并对剩余元素进行缩放。

\[\text{output}_i = \text{input}_i \cdot \text{mask}_i \cdot \text{scale}\]
输入:
  • input - 输入张量的数据地址。

  • scale - 缩放因子。

  • length - 输入张量的总元素数量。

  • mask - 预先生成的掩码张量的数据地址,其元素为0或1,大小与`input`相同。

  • core_mask - 核掩码。

输出:
  • output - 输出张量的数据地址,其大小与`input`相同。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持fp32

  • MT7004 支持fp16, fp32

共享存储版本:

void fp_dropout_s(float *input, float scale, int length, float *output, float mask, int core_mask)
void hp_dropout_s(half *input, half scale, int length, half *output, half *mask, int core_mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <dropout.h>
 4int main(int argc, char* argv[]) {
 5    float *input = (float *)0xA0000000;    // input 在DDR空间
 6    float *output = (float *)0xB0000000;   // output
 7    float *mask = (float *)0xC0000000;     // pre-generated mask
 8
 9    int length = 4096;
10    // 假设 dropout 概率 p = 0.2
11    float scale = 1.0f / (1.0f - 0.2f); // scale = 1.25
12    int core_mask = 0xff;
13
14    fp_dropout_s(input, scale, length, output, mask, core_mask);
15    return 0;
16}

私有存储版本:

void fp_dropout_p(float *input, float scale, int length, float *output, float mask)
void hp_dropout_p(half *input, half scale, int length, half *output, half mask)

C调用示例:

 1//FT78NE示例
 2#include <stdio.h>
 3#include <dropout.h>
 4int main(int argc, char* argv[]) {
 5    float *input = (float *)0x10000000;    // input 在L2空间
 6    float *output = (float *)0x11000000;   // output
 7    float *mask = (float *)0x12000000;     // pre-generated mask
 8
 9    int length = 1024;
10    // 假设 dropout 概率 p = 0.5
11    float scale = 1.0f / (1.0f - 0.5f); // scale = 2.0
12
13    fp_dropout_p(input, scale, length, output, mask);
14    return 0;
15}